package com.fc.remoting;

import org.springframework.stereotype.Component;

import com.fc.remoting.logger.RemotingLogger;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

@Component
public class TcpRemotingServerImpl extends AbstractRemotingServer{
	private EventLoopGroup bossGroup = new NioEventLoopGroup(1);
	private EventLoopGroup workerGroup = new NioEventLoopGroup();

	@Override
	public void start(int port) throws Exception {
		ServerBootstrap bootstrap = getDefaultServerBootstrap(bossGroup,workerGroup);
		bootstrap.childHandler(new TcpChannelInitializer()).bind(port).sync();
	}

	@Override
	public void shutdown(){
		try{
			workerGroup.shutdownGracefully();
			bossGroup.shutdownGracefully();
		}catch(Exception e){
			RemotingLogger.getLogger().error(e.getMessage(),e);
		}
	}
}
